package com.example.lenovo.demo.presenter;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Matrix;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.Overlay;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.map.PolylineOptions;
import com.baidu.mapapi.model.LatLng;
import com.example.lenovo.demo.MyApplication;
import com.example.lenovo.demo.R;
import com.example.lenovo.demo.model.databean.CityBean;
import com.example.lenovo.demo.model.databean.ModelDaysBean;
import com.example.lenovo.demo.presenter.adapter.MapFragment_ExpandableLvAdapter;
import com.example.lenovo.demo.view.ifgview.IMapFragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Lenovo on 2017/1/10.
 */

public class MapFgPresenter implements View.OnClickListener{
    private IMapFragment iMapFragment;
    private List<OverlayOptions> optionses;
    private List<LatLng> allPoints;
    private Bitmap bitmap;
    private LatLng latLng;
    private boolean type=false;
    private MapView mapView;
    private BitmapDescriptor bitmaps ;
    private Map<String,List<ModelDaysBean.DataBean.DaysBean.PointsBean>> data;
    private ExpandableListView expandableListView;
    private List<ModelDaysBean.DataBean.DaysBean> poins;
    private BaiduMap baiduMap;
    private RelativeLayout relativeLayout;
    private List<CityBean.DataBean.SectionsBean.ModelsBean.DaysBean> values;
    private String[] keys;
    private ImageView error;
    private TextView msg1,msg2;
    private RadioGroup rg;
    private ModelDaysBean modelDaysBean;


    public MapFgPresenter(IMapFragment iMapFragment) {
        this.iMapFragment = iMapFragment;
    }
    public void initData(){
        modelDaysBean=iMapFragment.getModelDaysBean();
        relativeLayout=iMapFragment.getRl();
        error= (ImageView) relativeLayout.findViewById(R.id.activity_map_error);
        msg1= (TextView) relativeLayout.findViewById(R.id.activity_map_msg1);
        msg2= (TextView) relativeLayout.findViewById(R.id.activity_map_msg2);
        rg= (RadioGroup) relativeLayout.findViewById(R.id.activity_map_rg);
        error.setOnClickListener(this);

        mapView=iMapFragment.getMapView();
        data=new HashMap<>();
        expandableListView=iMapFragment.getExpandableLv();

        keys=new String[modelDaysBean.getData().getDays().size()];
        values=new ArrayList<>();
        for (int i = 0; i < modelDaysBean.getData().getDays().size(); i++) {
            keys[i]="Day-"+(i+1)+"-"+modelDaysBean.getData().getDestination().getName();
            data.put(keys[i],modelDaysBean.getData().getDays().get(i).getPoints());
        }
        optionses=new ArrayList<>();
        poins=new ArrayList<>();
        allPoints=new ArrayList<>();
        baiduMap=mapView.getMap();
        initBitMap();
        MapFragment_ExpandableLvAdapter adapter=new MapFragment_ExpandableLvAdapter(keys,data,iMapFragment.myContext());
        expandableListView.setAdapter(adapter);
        optionses=new ArrayList<>();
        expandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView expandableListView, View view, int i, long l) {
                if(relativeLayout.getVisibility()==View.VISIBLE){
                    relativeLayout.setVisibility(View.GONE);
                }
                if(!expandableListView.isGroupExpanded(i)){

                    baiduMap.clear();
                    optionses.clear();
                    poins.clear();
                    allPoints.clear();
                    poins.add(modelDaysBean.getData().getDays().get(i));
                    for (ModelDaysBean.DataBean.DaysBean daysBean : poins) {
                        List<ModelDaysBean.DataBean.DaysBean.PointsBean> poins= daysBean.getPoints();
                        for (ModelDaysBean.DataBean.DaysBean.PointsBean poin : poins) {
                            latLng=new LatLng(poin.getPoi().getLat(),poin.getPoi().getLng());
                            OverlayOptions option = new MarkerOptions()
                                    .position(latLng)
                                    .icon(bitmaps);
                            optionses.add(option);
                            allPoints.add(latLng);
                        }
                    }
                    OverlayOptions options=new PolylineOptions().color(Color.RED).width(5).points(allPoints);

                    baiduMap.addOverlays(optionses);
                    baiduMap.addOverlay(options);
                    initMainLocation(mapView,allPoints.get(0),12);
                }

                return false;
            }
        });
        expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView expandableListView, View view, int i, int i1, long l) {
                LatLng latLng=new LatLng(data.get(keys[i]).get(i1).getPoi().getLat(),data.get(keys[i]).get(i1).getPoi().getLng());
                initMainLocation(mapView,latLng,16);
                msg1.setText(data.get(keys[i]).get(i1).getInspiration_activity().getTopic());
                msg2.setText(modelDaysBean.getData().getDestination().getName()+"•建议玩"+data.get(keys[i]).get(i1).getInspiration_activity().getVisit_tip());
//                Bitmap bitmap=initBitmap(R.drawable.location2);
//                BitmapDescriptor bitMap=BitmapDescriptorFactory.fromBitmap(bitmap);
//                MarkerOptions option= (MarkerOptions) optionses.get(i1);
//                option.icon(bitMap);
//                optionses.set(i1,option);
//                baiduMap.addOverlays(optionses);
                relativeLayout.setVisibility(View.VISIBLE);
                return false;
            }
        });


    }

    private void initBitMap() {
        bitmap= BitmapFactory.decodeResource(iMapFragment.myContext().getResources(), R.drawable.location);
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        // 设置想要的大小
        int newWidth = 40;
        int newHeight = 40;
        // 计算缩放比例
        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;
        // 取得想要缩放的matrix参数
        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth, scaleHeight);
        // 得到新的图片
        bitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix,
                true);
        bitmaps = BitmapDescriptorFactory.fromBitmap(bitmap);
    }


    private Bitmap initBitmap(int resouceID) {
        Bitmap bitmap= BitmapFactory.decodeResource(iMapFragment.myContext().getResources(), resouceID);
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        // 设置想要的大小
        int newWidth = 40;
        int newHeight = 40;
        // 计算缩放比例
        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;
        // 取得想要缩放的matrix参数
        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth, scaleHeight);
        // 得到新的图片
      return  bitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix,true);

    }

    private void initMainLocation(MapView mapView,LatLng cenpt,int zoom) {
        //定义地图状态
        MapStatus mMapStatus = new MapStatus.Builder()
                .target(cenpt)
                .zoom(zoom)
                .build();
        MapStatusUpdate mMapStatusUpdate = MapStatusUpdateFactory.newMapStatus(mMapStatus);
        mapView.getMap().setMapStatus(mMapStatusUpdate);
    }

    @Override
    public void onClick(View view) {
        relativeLayout.setVisibility(View.GONE);
    }
}

